package com.bjshiwei.zhengu.ui.fragment;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bjshiwei.zhengu.R;
import com.bjshiwei.zhengu.adapter.StrateVPAdapter;

import java.util.ArrayList;

/**
 * 作者：Administrator on 2017/2/13 0013 10:28
 * 邮箱：sanjins@126.com
 */


public class MarketFragment extends BaseFragment{

    private RadioGroup mRadioGroup;
    private ViewPager mViewPager;
    private RadioButton[] mRbs;
    private ImageView mImageViewEdit;
    private ImageView mImageViewMine;
    private static final int mTextCheckedColor = Color.parseColor("#0079f5");
    private static final int mTextUnCheckedColor = Color.WHITE;
    private ArrayList<Fragment> mFragments;
    private StrateVPAdapter mAdapter;

    @Override
    protected void setListener() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_market_one:
                        mViewPager.setCurrentItem(0);
                       /* mImageViewMine.setVisibility(View.VISIBLE);
                        mImageViewEdit.setVisibility(View.GONE);*/
                        break;
                    case R.id.rb_market_two:
                        mViewPager.setCurrentItem(1);
                        /*mImageViewMine.setVisibility(View.GONE);
                        mImageViewEdit.setVisibility(View.VISIBLE);*/
                        break;
                    default:
                        break;
                }

            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0 :
                        mRbs[position].setChecked(true);
                        mImageViewMine.setVisibility(View.VISIBLE);
                        mImageViewEdit.setVisibility(View.GONE);
                        break;
                    case 1:
                        mRbs[position].setChecked(true);
                        mImageViewMine.setVisibility(View.GONE);
                        mImageViewEdit.setVisibility(View.VISIBLE);
                        break;
                }
                setTextColor();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void setData() {
        mViewPager.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        mFragments = new ArrayList<>();
        mFragments.add(new SubMarketFragment());
        mFragments.add(new SubMineFragment());
        mAdapter = new StrateVPAdapter(getChildFragmentManager(), mFragments);
    }

    @Override
    protected View initView(LayoutInflater inflater) {
        View inflate = inflater.inflate(R.layout.fragment_market, null);
        return inflate;
    }

    @Override
    protected void initFindViewById(View view) {
        mRadioGroup = (RadioGroup) view.findViewById(R.id.rg_market_nav);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_market_body);
        mRbs = new RadioButton[2];
        mRbs[0] = (RadioButton) view.findViewById(R.id.rb_market_one);
        mRbs[1] = (RadioButton) view.findViewById(R.id.rb_market_two);
        mImageViewEdit = (ImageView) view.findViewById(R.id.tv_market_edit);
        mImageViewMine = (ImageView) view.findViewById(R.id.iv_market_mine);
        setTextColor();
    }

    private void setTextColor() {
        for (int i = 0; i < mRbs.length; i++) {
            if(mRbs[i].isChecked()) {
                mRbs[i].setTextColor(mTextCheckedColor);
            }else {
                mRbs[i].setTextColor(mTextUnCheckedColor);
            }
        }
    }
}
